package com.carlinfo.demo.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.carlinfo.common.util.ConstantFinalUtil;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.demo.dao.IADynastyDao;
import com.carlinfo.demo.dao.IAKingDao;
import com.carlinfo.demo.dao.impl.ADynastyDaoImpl;
import com.carlinfo.demo.dao.impl.AKingDaoImpl;
import com.carlinfo.demo.pojo.ADynasty;
import com.carlinfo.demo.pojo.AKing;
import com.carlinfo.demo.service.IDemoService;

public class DemoServiceImpl implements IDemoService
{
	IADynastyDao dynastyDao = new ADynastyDaoImpl();
	IAKingDao kingDao = new AKingDaoImpl();

	/*-------------------------朝代操作开始----------------------------------*/
	@Override
	public JSONObject saveOneDynastyService(ADynasty dynasty)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.dynastyDao.saveOneDao(dynasty);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", dynasty.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public List<ADynasty> selectListDynastyService(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		return this.dynastyDao.selectListDao(pageInfoUtil,condMap);
	}

	@Override
	public ADynasty selectOneDynastyService(Map<String, Object> condMap)
	{
		return this.dynastyDao.selectOneDao(condMap);
	}

	@Override
	public JSONObject updateOneDynastyService(ADynasty dynasty)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.dynastyDao.updateOneDao(dynasty);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", dynasty.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public JSONObject deleteOneDynastyService(Map<String, Object> condMap)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.dynastyDao.deleteOneDao(condMap);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	/*-------------------------朝代操作结束----------------------------------*/
	/*-------------------------皇上操作开始----------------------------------*/


	@Override
	public JSONObject saveOneKingService(AKing king)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.kingDao.saveOneDao(king);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", king.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}

	@Override
	public List<AKing> selectListKingService(PageInfoUtil pageInfoUtil, Map<String, Object> condMap)
	{
		List<AKing> list = this.kingDao.selectListDao(pageInfoUtil, condMap);
		if("true".equalsIgnoreCase(condMap.get("extend")+""))
		{
			for (AKing king : list)
			{
				Map<String, Object> map = new HashMap<String,Object>();
				ADynasty dynasty = this.dynastyDao.selectOneDao(map);
				king.setDynasty(dynasty);
			}
		}
		return list;
	}

	@Override
	public AKing selectOneKingService(Map<String, Object> condMap)
	{
		AKing king =this.kingDao.selectOneDao(condMap);
		if("true".equalsIgnoreCase(condMap.get("extend")+""))
		{
			Map<String, Object> map = new HashMap<String,Object>();
			ADynasty dynasty = this.dynastyDao.selectOneDao(map);
			king.setDynasty(dynasty);
		}
		return king;
	}

	@Override
	public JSONObject updateOneKingService(AKing king)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.kingDao.updateOneDao(king);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			dataJson.put("id", king.getId());
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	
	@Override
	public JSONObject deleteOneKingService(Map<String, Object> condMap)
	{
		JSONObject jsonObj = new JSONObject();
		/* 将字符串转为json对象 */
		int res = this.kingDao.deleteOneDao(condMap);
		if (res > 0)
		{
			jsonObj.put("code", "0");
			/* 将info中的数据取出来放到json中 */
			jsonObj.put("info",ConstantFinalUtil.INFO_JSON.get("0"));
			JSONObject dataJson = new JSONObject();
			dataJson.put("effect", res);
			jsonObj.put("data", dataJson);
		} else
		{
			jsonObj.put("code", "1");
			jsonObj.put("info", ConstantFinalUtil.INFO_JSON.get("1"));
		}
		return jsonObj;
	}
	/*-------------------------皇上操作结束----------------------------------*/

	
}
